import { reloadAuthorized } from './Authorized';

import { any } from 'prop-types';
import { ConsoleSqlOutlined } from '@ant-design/icons';

// use localStorage to store the authority info, which might be sent from server in actual project.
export function getAuthority(str?: string): string | string[] {
  let roleList = sessionStorage.getItem('authoMenuList')
    ? JSON.parse(sessionStorage.getItem('authoMenuList'))
    : [];
  let list = [];
  console.log('roleList', roleList);
  roleList.map((event) => {
    list.push(event.resourceCode);
  });
  const authorityString = list;
  let authority;
  try {
    if (authorityString) {
      authority = authorityString;
    }
  } catch (e) {
    authority = authorityString;
  }
  if (typeof authority === 'string') {
    return [authority];
  }
  console.log('权限列表数据', authority, 'authority');
  console.log('list', list);
  return authority;

  // const authorityString =
  //   typeof str === 'undefined' && localStorage ? localStorage.getItem('antd-pro-authority') : str;
  // // authorityString could be admin, "admin", ["admin"]
  // let authority;
  // try {
  //   if (authorityString) {
  //     authority = JSON.parse(authorityString);
  //   }
  // } catch (e) {
  //   authority = authorityString;
  // }
  // if (typeof authority === 'string') {
  //   return [authority];
  // }
  // // preview.pro.ant.design only do not use in your production.
  // // preview.pro.ant.design 专用环境变量，请不要在你的项目中使用它。
  // if (!authority && ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
  //   return ['admin'];
  // }
  // return authority;
  // let menuList = getSessionStorage('authoMenuList', true);
  // const menuList = sessionStorage.getItem('authoMenuList') ? JSON.parse(sessionStorage.getItem('authoMenuList')) : [];
  // let menuData = [];
  // if (menuList && menuList.length > 0) {
  //   for (let i = 0; i < menuList.length; i++) {
  //     let item = menuList[i];
  //     menuData.push(item.resourceCode);
  //   }
  // }
  // return menuData;
}

export function setAuthority(authority: string | string[]): void {
  const proAuthority = typeof authority === 'string' ? [authority] : authority;
  localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority));
  // auto reload
  reloadAuthorized();
}

export function getSessionStorage(param?: any) {
  let localData = '';

  for (const item in window.sessionStorage) {
    if (window.sessionStorage.hasOwnProperty(param) && param == item) {
      if (
        window.sessionStorage.getItem(item) === 'undefined' ||
        window.sessionStorage.getItem(item) === null
      ) {
        localData = '';
      } else {
        localData = JSON.parse(window.sessionStorage.getItem(item));
      }
    }
  }

  return localData;
}

//获取本地存储
export function getLocalStroage(param?: any) {
  let localData = '';

  for (const item in window.localStorage) {
    if (window.localStorage.hasOwnProperty(param) && param == item) {
      if (
        window.localStorage.getItem(item) === 'undefined' ||
        window.localStorage.getItem(item) === null ||
        window.localStorage.getItem(item) === '[]'
      ) {
        localData = '';
      } else {
        localData = JSON.parse(window.localStorage.getItem(item));
      }
    }
  }

  return localData;
}

// 获取当前办案区信息
export function getcurrentBAQ(param?: any) {
  const currentBAQInfo =
    sessionStorage.getItem('currentBAQInfo') === null ||
    sessionStorage.getItem('currentBAQInfo') === 'undefined'
      ? ''
      : JSON.parse(sessionStorage.getItem('currentBAQInfo'));

  if (currentBAQInfo) {
    return currentBAQInfo[param] || '';
  }

  return '';
}

// 获取当前办案区动态表单配置
export function getConfigInfo(param?: any) {
  const configInfo =
    sessionStorage.getItem('configInfo') === null ||
    sessionStorage.getItem('configInfo') === 'undefined'
      ? ''
      : JSON.parse(sessionStorage.getItem('configInfo'));
  if (configInfo) {
    return configInfo[param];
  }
  return '';
}

// 获取当前办案区服务id
export function getServiceInfo(param?: any) {
  const serviceInfo =
    sessionStorage.getItem('serviceInfo') === null ||
    sessionStorage.getItem('serviceInfo') === 'undefined'
      ? ''
      : JSON.parse(sessionStorage.getItem('serviceInfo'));
  if (serviceInfo) {
    let serviceId = '';
    serviceInfo.map((item) => {
      if (item.serviceType === param) {
        serviceId = item.serviceId;
      }
    });
    return serviceId;
  }
  return '';
}

export function getSysAuthority(param?: any) {
  const sysConfig =
    localStorage.getItem('sysConfig') === null || localStorage.getItem('sysConfig') === 'undefined'
      ? []
      : JSON.parse(localStorage.getItem('sysConfig')); // 权限列表
  if (sysConfig) {
    return sysConfig[param];
  }
  return '';
  // return Object.getOwnPropertyDescriptor(sysConfig, param).value;
}

//获取当前登录人的数据详情

export function getcurrentUser(param?: any) {
  const currentUserInfo =
    sessionStorage.getItem('user') === null || sessionStorage.getItem('user') === 'undefined'
      ? ''
      : JSON.parse(sessionStorage.getItem('user'));

  if (currentUserInfo) {
    return currentUserInfo[param] || '';
  }

  return '';
}

// 通过权限编码查询权限列表获取是否有此权限，返回true或false
export function authorityIsTrue(code?: any) {
  let isTrue = false;
  const authoMenuList =
    sessionStorage.getItem('authoMenuList') &&
    sessionStorage.getItem('authoMenuList') === 'undefined'
      ? []
      : JSON.parse(sessionStorage.getItem('authoMenuList')); // 权限列表

  if (authoMenuList && authoMenuList.length > 0) {
    for (let i = 0; i < authoMenuList.length; i++) {
      const menu = authoMenuList[i];

      if (menu.resourceCode === code) {
        isTrue = true;
      }
    }
  }

  return isTrue;
}
